package com.share.order.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 延迟消息配置类
 * 基于rabbit 延迟消息插件实现
 */
@Configuration
public class DelayedMqConfig {

    //死信交换机 队列 routing key
    public static final String exchange_delay = "exchange.delay";
    public static final String routing_delay = "routing.delay";
    public static final String queue_delay_1 = "queue.delay.1";

    //队列 bean
    @Bean
    public Queue delayQeue1() {
        // 参数 queue名字，是否持久化
        return new Queue(queue_delay_1, true);
    }

    //交换机 bean
    @Bean
    public CustomExchange delayExchange(){
        Map<String, Object> args = new HashMap<String, Object>();

        //设置延迟参数 x-delayed-type
        args.put("x-delayed-type","direct");

        //参数： 交换机名 交换机类型指定为延迟消息交换机 是否持久化 是否自动删除消息 设置的参数
        return new CustomExchange(exchange_delay,"x-delayed-message",true,false,args);
    }

    //设置binding key
    @Bean
    public Binding delayBbinding1() {
        return BindingBuilder
                .bind(delayQeue1()) //队列
                .to(delayExchange()) //交换机
                .with(routing_delay) //routing key
                .noargs(); //无额外参数
    }

}
